import 'dart:io';

import 'package:adaptive_theme/adaptive_theme.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:hive_flutter/adapters.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'const/app_policies.dart';
import 'core/core_intl/core_kit_localizations.dart';
import 'core/toast.dart';
import 'generated/l10n.dart';
import 'router/app_router.dart';
import 'service/app_service.dart';
import 'utils/encryp_utils.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await initApp();

  runApp(const ProviderScope(child: FlutterBaseApp()));
}

//初始化App调用
Future<void> initApp() async {
  // //开启代理抓包
  // HttpProxyOverride httpProxyOverride =
  //     await HttpProxyOverride.createHttpProxy();
  // if (Env.appEnv != EnvName.release) {
  //   if (httpProxyOverride.host != null && httpProxyOverride.port != null) {
  //     HttpOverrides.global = httpProxyOverride;
  //   }
  // }

  // 初始化数据库，必须放在最前面
  await Hive.initFlutter();

  //图片缓存
  AppPolicies.imageCacheBox = await Hive.openBox('imageCache');
  //视频缓存
  AppPolicies.videoCacheBox = await Hive.openBox('videoCacheBox');

  //初始化加密信息
  await EncryptUtils.initAes();

  //強制app豎屏
  await SystemChrome.setPreferredOrientations(<DeviceOrientation>[
    DeviceOrientation.portraitUp,
    // DeviceOrientation.portraitDown
  ]);

  if (!kIsWeb && Platform.isAndroid) {
    const systemUiOverlayStyle = SystemUiOverlayStyle(
      statusBarColor: Colors.transparent, //设置为透明
      statusBarIconBrightness: Brightness.light,
      systemNavigationBarColor: Colors.transparent,
    );
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
    await SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
  }
}

class FlutterBaseApp extends ConsumerWidget {
  const FlutterBaseApp({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return AdaptiveTheme(
      light: ThemeData.light(),
      dark: ThemeData.dark(),
      initial: AdaptiveThemeMode.system,
      builder: (theme, darkTheme) {
        final locale = ref.watch(appLocalizationsProvider);

        return MaterialApp.router(
          title: 'FlutterBase',
          builder: (context, child) {
            return CoreKitToastInit()(
              context,
              Material(
                child: CupertinoAdaptiveTheme(
                  light: const CupertinoThemeData(brightness: Brightness.light),
                  dark: const CupertinoThemeData(brightness: Brightness.dark),
                  initial: AdaptiveThemeMode.system,
                  builder: (cupertinoThemeDate) => CupertinoTheme(
                    data: cupertinoThemeDate,
                    child: child ?? Container(),
                  ),
                ),
              ),
            );
          },
          localizationsDelegates: const <LocalizationsDelegate<dynamic>>[
            GlobalCupertinoLocalizations.delegate,
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            RefreshLocalizations.delegate,
            CoreKitLocalizations.delegate,
            AppIntl.delegate,
          ],
          locale: locale,
          supportedLocales: AppIntl.delegate.supportedLocales,
          theme: theme,
          darkTheme: darkTheme,
          routerConfig: appRouter,
        );
      },
    );
  }
}
